package com.oyjd.ybx.business.goods.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.ibm.icu.text.SimpleDateFormat;
import com.oyjd.ybx.bean.po.Classify;
import com.oyjd.ybx.bean.po.GoodsCount;
import com.oyjd.ybx.bean.po.GoodsInfo;
import com.oyjd.ybx.bean.vo.ClassifyVO;
import com.oyjd.ybx.business.goods.dao.GoodsDao;
import com.oyjd.ybx.business.goods.service.GoodsService;
import com.oyjd.ybx.business.goods.vo.GoodsInfoExVO;
import com.oyjd.ybx.fw.action.Page;

/**
 * 
 * 商品信息业务接口实现
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Component
@Transactional(readOnly = true)
public class GoodsServiceImpl implements GoodsService {
	private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHH");
	private GoodsDao dao;

	@Autowired
	public void setDao(GoodsDao dao) {
		this.dao = dao;
	}

	/**
	 * 获取商品信息列表数据
	 * 
	 * @param page
	 *            页面对象
	 * @param user
	 *            当前用户
	 * @return
	 */
	public Page<GoodsInfoExVO> getList(Page<GoodsInfoExVO> page, String user) {
		return dao.getList(page, user);
	}

	/**
	 * 获取前端大类商品信息展示列表
	 * 
	 * @param page
	 * @return
	 */
	public Page<GoodsInfoExVO> getFrontList(Page<GoodsInfoExVO> page) {
		return dao.getFrontList(page);
	}

	/**
	 * 根据ID获取商品信息对象信息
	 * 
	 * @param id
	 *            ID
	 * @return
	 */
	public GoodsInfoExVO getGoodsInfoVO(String goodsId) {
		return dao.getGoodsInfoVO(goodsId);
	}

	/**
	 * 统计浏览量
	 * 
	 * @param goodsId
	 *            物品ID
	 * @param ip
	 *            请求地址
	 */
	@SuppressWarnings("rawtypes")
	@Transactional(readOnly = false)
	public void countLook(String goodsId, String ip) {
		String key = ip + "_" + sdf.format(new Date());
		List tmp = dao.queryByHql("from " + GoodsCount.class.getName() + " where goodsId=? and ipTime=?", new Object[] { goodsId, key });
		if (0 == tmp.size()) {
			GoodsCount gc = new GoodsCount();
			gc.setGoodsId(goodsId);
			gc.setIpTime(key);
			dao.create(gc);

			dao.executeHql("update " + GoodsInfo.class.getName() + " g set g.countLook=(select count(t.id) from " + GoodsCount.class.getName() + " t where t.goodsId=g.goodsId) where g.goodsId=?", new Object[] { goodsId });
		}
	}

	/**
	 * 增加商品信息
	 * 
	 * @param ageId
	 *            年龄段主键
	 * @param classId
	 *            分类ID
	 * @param goodsName
	 *            商品名称
	 * @param goodsPicName
	 *            商品图片名称
	 * @param goodsPic
	 *            商品图片
	 * @param recommend
	 *            推荐理由
	 * @param link
	 *            原始链接
	 */
	@Transactional(readOnly = false)
	public void add(String ageId, String classId, String goodsName, String goodsPic, String recommend, String link, String user) throws Exception {
		Date date = new Date();
		GoodsInfo goodsInfo = new GoodsInfo();
		goodsInfo.setAgeId(ageId);
		goodsInfo.setClassId(classId);
		goodsInfo.setGoodsName(goodsName);
		goodsInfo.setGoodsPic(goodsPic);
		goodsInfo.setRecommend(recommend);
		goodsInfo.setLink(link);
		goodsInfo.setCreator(user);
		goodsInfo.setCreateTime(date);
		goodsInfo.setStatus(0);
		goodsInfo.setOrderKey(System.currentTimeMillis());
		dao.create(goodsInfo);
		getNewGoodsCountByDb();
	}

	/**
	 * 修改商品信息
	 * 
	 * @param goodsId
	 *            商品ID
	 * @param ageId
	 *            年龄段主键
	 * @param classId
	 *            分类ID
	 * @param goodsName
	 *            商品名称
	 * @param goodsPicName
	 *            商品图片名称
	 * @param goodsPic
	 *            商品图片
	 * @param recommend
	 *            推荐理由
	 * @param link
	 *            原始链接
	 */
	@Transactional(readOnly = false)
	public void edit(String goodsId, String ageId, String classId, String goodsName, String goodsPic, String recommend, String link) throws Exception {
		GoodsInfo goodsInfo = (GoodsInfo) dao.queryById(GoodsInfo.class, goodsId);
		goodsInfo.setGoodsId(goodsId);
		goodsInfo.setAgeId(ageId);
		goodsInfo.setClassId(classId);
		goodsInfo.setGoodsName(goodsName);
		goodsInfo.setGoodsPic(goodsPic);
		goodsInfo.setRecommend(recommend);
		goodsInfo.setLink(link);
		dao.update(goodsInfo);
		getNewGoodsCountByDb();
	}

	/**
	 * 删除商品信息
	 * 
	 * @param ids
	 *            多个ID
	 * @param user
	 *            当前用户
	 * @return
	 */
	@Transactional(readOnly = false)
	public void remove(String[] ids) {
		dao.remove(ids);
		getNewGoodsCountByDb();
	}

	/**
	 * 更改商品信息状态
	 * 
	 * @param ids
	 *            多个ID
	 * @param status
	 *            状态
	 * @param user
	 *            当前用户
	 * @return
	 */
	@Transactional(readOnly = false)
	public void updateStatus(String[] ids, int status, String user) {
		dao.updateStatus(ids, status, user);
	}

	private Map<String, Integer> newCountMap = null;

	/**
	 * 获取最新上线物品数
	 * 
	 * @return
	 */
	public Map<String, Integer> getNewGoodsCount() {
		if (null == newCountMap) {
			getNewGoodsCountByDb();
		}
		return newCountMap;
	}

	/**
	 * 获取最新上线物品数从数据库中
	 * 
	 * @return
	 */
	public void getNewGoodsCountByDb() {
		Map<String, Integer> map = new LinkedHashMap<String, Integer>();
		String[] ids = { "A000", "B000", "C000", "D000" };
		for (String id : ids) {
			map.put(id, dao.getNewGoodsCount(id));
		}
		newCountMap = map;
	}

	/**
	 * 获取热门的第三分类
	 * 
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Map<String, List<ClassifyVO>> getHotClass3() {
		Map<String, List<ClassifyVO>> result = new HashMap<String, List<ClassifyVO>>();

		String[] ids = { "A000", "B000", "C000", "D000" };
		int[] nums = { 8, 6, 4, 4 };

		String hql = "select a.classId,c.classId from " + Classify.class.getName() + " a," + Classify.class.getName() + " b," + Classify.class.getName() + " c where a.classId=b.pid and b.classId=c.pid and a.pid =''";
		Map<String, List<String>> m = new HashMap<String, List<String>>();
		List<Object[]> objs = (List<Object[]>) dao.queryByHql(hql);
		for (Object[] o : objs) {
			String a1 = (String) o[0];
			String a2 = (String) o[1];
			if (m.containsKey(a1)) {
				m.get(a1).add(a2);
			} else {
				List<String> tmp = new ArrayList<String>();
				tmp.add(a2);
				m.put(a1, tmp);
			}
		}

		String hql2 = "select new " + ClassifyVO.class.getName() + ClassifyVO.STRUCT + " from " + GoodsInfo.class.getName() + " g," + Classify.class.getName() + " classify where g.classId=classify.classId group by classify order by sum(g.countLike) desc";
		List<ClassifyVO> cs = (List<ClassifyVO>) dao.queryByHql(hql2);
		for (int i = 0; i < ids.length; i++) {
			List<String> list = m.get(ids[i]);
			List<ClassifyVO> tmp = new ArrayList<ClassifyVO>();
			for (ClassifyVO vo : cs) {
				if (list.contains(vo.getClassId())) {
					tmp.add(vo);
				}
				if (tmp.size() >= nums[i]) {
					break;
				}
			}
			result.put(ids[i], tmp);
		}
		return result;
	}
}
